package ClassPack;

import java.util.Vector;

import Composotion.BasketClassPair;
import basketMatcher.Basket;

public class ClassDiagram {
 
	private Vector<Class> 		m_classes;
	private String 				Name;
	private Vector<BasketClassPair> m_classesAndBaskets;

	public ClassDiagram() {
		m_classes = new Vector<Class>();
		m_classesAndBaskets = new Vector<BasketClassPair>();
	}


	public String toString() {
		
		String ans = new String();
		ans += Name + ": \n";
		
		for ( Class c : m_classes) {
			ans+= c.toString();
		}
		return ans;
		
	}
	
	/**
	 * 	connects between the new classes in a meta class diagram.
	 * the main idea: if two classes have connected classes in their baskets ,
	 * they'll be connected as referenced. ( wer'e assuming a wide base mutual 
	 */
	public void connectClassesToOtherClasses(){

		for ( BasketClassPair pair1: m_classesAndBaskets) {
			for ( BasketClassPair pair2: m_classesAndBaskets ) {
				
				if (pair1 != pair2){
					
					Basket basket1 = pair1.getM_basket();
					Basket basket2 = pair2.getM_basket();
					
				boolean break_flag = false;
				for (Class c1 : basket1.getM_classes()   ) {
				
					for(Class referenced_class : c1.getReferenced_classes()){
						
						if ( basket2.getM_classes().contains(referenced_class)){
	//						if ( ! bc_pair1.getM_class().getReferenced_classes().contains(referenced_class) )
							pair1.getM_class().addReferencedClass(pair2.getM_class());
							break_flag = true;
						}
						if (break_flag)
							break;
					}
					if (break_flag)
						break;
				}
			}
		}	
	}

	}
	/**
	 *  same of before, only for interfaces.
	 */

	public void connectClassesInterfaces(){

		for ( BasketClassPair pair1: m_classesAndBaskets) {
			for ( BasketClassPair pair2: m_classesAndBaskets ) {

				if (pair1 != pair2){

					Basket basket1 = pair1.getM_basket();
					Basket basket2 = pair2.getM_basket();

					boolean break_flag = false;
					for (Class inheriting_class : basket1.getM_classes()   ) {
					
						
						for(Class inhereted_interface : basket2.getM_classes()){

								if ( inheriting_class.getM_inhereted_interfaces().contains(inhereted_interface)){
										//						if ( ! bc_pair1.getM_class().getReferenced_classes().contains(referenced_class) )
									pair1.getM_class().add_inhereted_interface(pair2.getM_class());
									break_flag = true;
								}
								if (break_flag)
									break;
								}
								if (break_flag)
									break;
							}
						}
					}	
				}
			}
	

	public void addClass(Class a1) {
		m_classes.add(a1);

	}











	public void addClass(Class a, Basket first_basket) {
		m_classes.add(a);
		m_classesAndBaskets.add(new BasketClassPair(a , first_basket));

	}

	
	
	
	
	
	
	
	
	
	public Vector<Class> getM_classes() {
		return m_classes;
	}
	public void setM_classes(Vector<Class> m_classes) {
		this.m_classes = m_classes;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}


	public Class getClassByName(String name2) {
		
		for (Class c : m_classes ) { 
			if (c.getM_class_name().equals(name2))
				return c;
		}
		
		return null;
	}


	
		
	
}
